﻿using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.Controllers;
using System.Reflection;
using Stock.Model;
using System.Net.Http;
using System.Net;

namespace Web.Filters
{

    public class UserData
    {
        public string userid { get; set; }
        public DateTime loginTime { get; set; }
        public string Token { get; set; }
    }

    public class LoginManager 
    {
        public static List<UserData> OnlineUser = new List<UserData>();

        public static string addUser(string userid)
        {
			string token = Guid.NewGuid().ToString();
			var c = OnlineUser.Find(a => a.userid == userid);
			if (c != null)
            {
                c.Token = token;
                c.loginTime = DateTime.Now;
            }
            else
                OnlineUser.Add(new UserData() { loginTime = DateTime.Now, Token = token, userid = userid });

            return token;
        }

        public static bool ValidateUserTicket(string token)
        {
            var c = OnlineUser.Find(a => a.Token == token);
            if (c != null)
            {
                return true;
            }
            else
                return false;
        }
    }

    public class RequireLoginAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            //var securityManager = context.HttpContext.RequestServices.GetService<SecurityManager>();
            
            var controllerActionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;
            if (controllerActionDescriptor != null)
            {
                var actionAttributes = controllerActionDescriptor.MethodInfo.GetCustomAttributes<AllowAnonymousAttribute>().OfType<AllowAnonymousAttribute>();
                bool isAnonymous = actionAttributes.Any(a => a is AllowAnonymousAttribute);
                if (isAnonymous)
                {
                    base.OnActionExecuting(context);
                }
            }

            // islogin

            var token = context.HttpContext.Request.Headers.FirstOrDefault(a => a.Key == "Authorization");
            if(token.Value.ToString().Length > 0)
            {
                if(!LoginManager.ValidateUserTicket(token.Value.ToString()))
                {
                    //context..Response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
                    //context.Result = 
                }
            }


            bool IsLogin = true;
            if (IsLogin)
            {
                base.OnActionExecuting(context);
            }
            else
            {
                string url = this.GetRedirectUrl(context.HttpContext.Request);

                context.Result = new RedirectToActionResult("Login", "Account", new { redirect = url });
            }
        }

        private string GetRedirectUrl(HttpRequest request)
        {
            var builder = new UriBuilder()
            {
                Path = request.Path,
                Query = request.QueryString.ToUriComponent()
            };

            return builder.Uri.PathAndQuery;
        }

    }
}
